/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.logging;

import org.testis.data.AtofModuleConstants;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Timer;
import java.util.TimerTask;

import org.testis.utils.middleware.LogDetail;
import org.testis.utils.middleware.LogHeader;


/**
 *
 * @author nurhat
 */
public class LogProviderService
{
    TimerTask task=new TimerTask(){
        public void run(){
           LogProviderService.getInstance().commitMessages();
        }
    };
    private static LogProviderService m_service;
    private LogDetail m_detail = new LogDetail();
    private LogHeader m_header = new LogHeader();
    private Timer t;

    private LogProviderService()
    {
        t=new Timer();
        t.schedule(task,100,10000);
    }


    public synchronized static LogProviderService getInstance()
    {
        if (m_service == null) {
            m_service = new LogProviderService();
        }

        return m_service;
    }

    public synchronized void commitMessages()
     {
        m_header.commit();
        m_detail.commit();
    }
    public synchronized void processMessage(LogData data)
    {
        try {
            if (data instanceof LogDetailData) {
                LogDetailData logDetailData = (LogDetailData) data;
              //  System.out.println(logDetailData);
                m_detail.addLog(logDetailData.getLoggingClass(),
                                logDetailData.getLevel(),
                                logDetailData.getLogMsg(),
                                logDetailData.getLoggingObjDesc(),
                                logDetailData.getDate(),
                                logDetailData.getGuid());
            }
            else if (data instanceof LogHeaderData) {
                LogHeaderData logHeaderData = (LogHeaderData) data;
                //System.out.println(logHeaderData);
                if (logHeaderData.getParentId() != 0) {
                    m_header.addLog(logHeaderData.getDesignTreeId(),
                                    logHeaderData.getDesignPartType(),
                                    logHeaderData.getResult().intValue(),
                                    logHeaderData.getParentId(),
                                    logHeaderData.getName(),
                                    logHeaderData.getDate(),
                                    logHeaderData.getGuid(),
                                    logHeaderData.getExecutionGuid());
                }
                else {
                    m_header.addLog(logHeaderData.getDesignTreeId(),
                                    logHeaderData.getDesignPartType(),
                                    logHeaderData.getResult().intValue(),
                                    logHeaderData.getParentGuid(),
                                    logHeaderData.getName(),
                                    logHeaderData.getDate(),
                                    logHeaderData.getGuid(),
                                    logHeaderData.getExecutionGuid());
                }
            }
            else if (data instanceof FileLogData) {
                FileLogData fileLogData = (FileLogData) data;
                //store to file
                String envPath = AtofModuleConstants.ATOF_CLIENT_HOME;
                String logFolder = envPath + "/Logs/";
                String logFile = logFolder + fileLogData.getGuid() +
                    ".txt";
                try {
                    File folder = new File(logFolder);
                    if (!folder.exists()) {
                        folder.mkdir();
                    }
                    FileWriter fstream = new FileWriter(logFile,
                                                        true);
                    BufferedWriter out = new BufferedWriter(fstream);
                    out.write(fileLogData.toString());
                    out.close();
                } catch (Exception e) {//Catch exception if any
                    System.out.println("FileLogData Error:");
                        e.printStackTrace();
                /*    System.out.println("*************************************");
                    e.printStackTrace();
                    System.out.println("FileLogData Error: " + e.getMessage());
                    System.out.println("*************************************");*/
                }
            }
        } catch (Exception ex) {
            System.out.println("Error occured in log server...");
            ex.printStackTrace();
        }
    }


    public synchronized void processMessageForClient(LogData data)
    {
        data.printMessage();
    }


    @Override
    protected void finalize() throws Throwable
    {
        super.finalize(); 
        m_detail.closeDBConnection();
        m_header.closeDBConnection();
    }
}
